<?php
defined('BASEPATH') or die('forbidden');
class Mail_Controller Extends Controller
{
	private $mailType;	//邮件类型
	//private $objectlist;	//物品类型
	//private $playerList;	//玩家姓名
	private $dbConnt;	//数据库连接
	private $mailsourcelist;	//邮件源
	private $nameList;
	private $state;
	
	function _init()
	{
		check_level(50,segment(0),segment(1));
		set_time_limit(0);
		
		//$this->mailType=array("0"=>"群发邮件","1"=>"玩家之间普通邮件",'2'=>'系统单发邮件','3'=>'宠物邮件');
		$this->mailsourcelist = get_type_mailsource();
		//$this->objectlist = get_type_object_db();
		$this->state = array('0' => '已读', '1' => '未读', '2' => '已发送');
		
		//得到选择的区号
		$areanum = $_SESSION['area_selected_id'];
        if ($areanum == '') 
        {
            echo "<script>alert('" . translate('C_SELECT_AREA_TEXT') . "');</script>";
            return;
        }
        // db数据库
		$this->dbConnt = getTmpDbConnect(PLATFORM_ALL, 'all', $areanum, DBTYPE_GAMESERVER);
		
		if (false == $this->dbConnt) {
			//内部有提示错误，在此就不提示了
			 //echo "<script>alert(' connect areanum:$areanum error ');</script>";
            return;
		}	
		$this->dbConnt->query("SELECT id, name FROM player");
		while ($n = $this->dbConnt->get_array('default'))
		{
			$this->nameList[$n['id']] = $n['name'];
		}
		
	}

	function index()
	{
		$this->view->display("dbmail/mail/index");
	}
	function flexigrid()
	{
		list($colkey, $colsinfo, $where, $sortname, $sortorder, $offset, $rp, $page) = $this->common->get_flexigrid_params();
		//$areanum = $_SESSION['area_selected_id'];
		//echo " areanum:$areanum where = $where colkey = $colkey ";
		
		
		$sql = "SELECT COUNT(*) AS num FROM player_mail  WHERE {$where} {$groupBy}";
		//echo " sql2:$sql ";
		$one = $this->dbConnt->one($sql);
		$total = $one['num'];
		$orderby = $sortname ? "ORDER BY {$sortname} {$sortorder} " : "";
		 
		$sql = "SELECT {$colsinfo} FROM player_mail  WHERE {$where}  {$groupBy} {$orderby} LIMIT {$offset},{$rp}";
		$this->dbConnt->query($sql);
		//$id = 0;
		while ($n = $this->dbConnt->get_array('default'))
		{
			$n['role_id'] = $this->nameList[$n['role_id']]."(".$n['role_id'].")";
			$n['state'] = $this->state[$n['state']]."(".$n['state'].")";
			$n['mail_source']=$this->mailsourcelist[$n['mail_source']][chinaname]."-".$n['mail_source'];
			$rows[] = array('id' => $n['id'], 'cell' => $n);
			//$id = $id+1;
		}
		//var_dump($rows);
		$result = array(
	            'page' => $page,
	            'total' => $total,
	            'rows' => $rows
		);
		//$this->dbConnt->free_all();
		echo json_encode($result);
	}
	
	//传来的id是邮件中的id
	function detail($id)
	{
		$sql = "select * from player_mail as A, player_mail_data as B where A.mail_id=B.gmid and A.id = $id ";
		//echo " sql:$sql ";
		$this->dbConnt->query($sql);
		//$id = 0;
		$data1 = $this->dbConnt->get_array('default');
		if(false == $data1)
		{
			//print_r($n);	
			//echo "<script>alert('" . translate('C_SELECT_AREA_TEXT') . "');</script>";	
			echo " can not find mail sql:$sql ";	
			return;
		}
		//此字段需要转下
		$data1['type']=$this->mailType[$data1['type']];
		
		//再查下得到的物品
		$sql = "select * from player_mail as A, player_mail_item as B where A.mail_id=B.mail_gmid and A.id = $id ";
		//echo " sql:$sql ";
		$this->dbConnt->query($sql);
		//$id = 0;
		$data2 = array();
		while( $n = $this->dbConnt->get_array('default'))
		{
			//print_r($n);	
			$n['type_id']=$this->objectType[$n['type_id']];
			$n['indate']=get_datetime($n['indate']);
			
			$data2[$n['gmid']]=$n;
		}
		//此字段需要转下
		
		//var_dump($data2);
		
		//print_r($n);
		$this->view->assign('mailData',$data1);
		$this->view->assign('mailItem',$data2);
		$this->view->display("message/mail/detail");
		
		$this->dbConnt->free_all();
		
	}
	
	function replay($id)
	{
		echo " id = $id ";
		
	}
}
?>